/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.pool.modelo;

import com.isd.facturacion.modelo.MovimientoTienda;
import com.isd.venta.modelo.Cliente;
import com.isd.venta.modelo.CondicionDePago;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author ctrosch
 */
@Entity
@Table(name = "USR_POOLRS")
@IdClass(ReservasPoolPK.class)
public class ReservasPool implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "USR_POOLRS_POOL_NRPOOL", nullable = false)
    private int nroPool;

    @Id
    @Column(name = "USR_POOLRS_NORDEN", nullable = false, length = 13)
    private int orden;
    
    @Id
    @Column(name = "USR_POOLRS_NROCTA", nullable = false, length = 13)
    private String nroCuenta;

    //Pool
    @ManyToOne
    @JoinColumn(name = "USR_POOLRS_NROCTA", referencedColumnName = "VTMCLH_NROCTA", nullable = false, insertable=false, updatable=false)
    private Cliente cliente;

    //Pool
    @ManyToOne
    @JoinColumn(name = "USR_POOLRS_POOL_NRPOOL", referencedColumnName = "USR_POOL_NRPOOL", nullable = false, insertable=false, updatable=false)
    private Pool pool;

    //Condicion de pago
    @JoinColumn(name = "USR_POOLRS_CNDPAG", referencedColumnName = "VTTCPH_CNDPAG", nullable = false)
    @ManyToOne(optional=false)
    private CondicionDePago condicionDePago;

    @Lob
    @Column(name = "USR_POOLRS_TEXTOS", length = 2147483647)
    private String textos;
        
    @Column(name = "USR_POOL_ESTADO", nullable = false, length = 20)
    private String estado;

    @JoinColumns({
        @JoinColumn(name = "USR_POOLRS_MODFOR", referencedColumnName = "FCRMVH_MODFOR", nullable = false),
        @JoinColumn(name = "USR_POOLRS_CODFOR", referencedColumnName = "FCRMVH_CODFOR", nullable = false),
        @JoinColumn(name = "USR_POOLRS_NROFOR", referencedColumnName = "FCRMVH_NROFOR", nullable = false)
    })
    @ManyToOne
    MovimientoTienda pedido;
    
    @Column(name = "USR_PO_FECALT")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fecalt;
    @Column(name = "USR_PO_FECMOD")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fecmod;
    @Column(name = "USR_PO_USERID", length = 15)
    private String userId;
    @Column(name = "USR_PO_ULTOPR", length=1)
    private String ultopr;
    @Column(name = "USR_PO_DEBAJA", length=1)
    private String debaja;
    @Column(name = "USR_PO_OALIAS", length = 10)
    private String oalias;

    @OneToMany(mappedBy = "reserva")
    private List<ItemReserva> itemReservas;

    @Transient
    private BigDecimal totalReservaConIVA;

    public ReservasPool() {

        this.fecalt = new Date();
        this.fecmod = new Date();
        this.userId = "TIVI";
        this.ultopr = "A";
        this.debaja = "N";
        this.oalias = "USR_POOLRS";
        this.estado = EstadoReserva.NoConfirmado;
        this.itemReservas = new ArrayList<ItemReserva>();

    }

    public ReservasPool(Pool pool) {
        this.pool = pool;
        this.nroPool = pool.getNroPool();
        this.orden = pool.getReservas().size();
        this.fecalt = new Date();
        this.fecmod = new Date();
        this.userId = "TIVI";
        this.ultopr = "A";
        this.debaja = "N";
        this.oalias = "USR_POOLRS";
        this.estado = EstadoReserva.NoConfirmado;
        this.itemReservas = new ArrayList<ItemReserva>();
    }

    public ReservasPool(String nroCuenta, Pool pool) {

        this.nroCuenta = nroCuenta;
        this.nroPool = pool.getNroPool();
        //this.orden = pool.getReservas().size();
        this.pool = pool;
        this.fecalt = new Date();
        this.fecmod = new Date();
        this.userId = "TIVI";
        this.ultopr = "A";
        this.debaja = "N";
        this.oalias = "USR_POOLRS";
        this.estado = EstadoReserva.NoConfirmado;
        this.itemReservas = new ArrayList<ItemReserva>();
    }

    public Pool getPool() {
        return pool;
    }

    public void setPool(Pool pool) {
        this.pool = pool;
    }

    public int getOrden() {
        return orden;
    }

    public void setOrden(int orden) {
        this.orden = orden;
    }
 
    public CondicionDePago getCondicionDePago() {
        return condicionDePago;
    }

    public void setCondicionDePago(CondicionDePago condicionDePago) {
        this.condicionDePago = condicionDePago;
    }

    public String getNroCuenta() {
        return nroCuenta;
    }

    public void setNroCuenta(String nroCuenta) {
        this.nroCuenta = nroCuenta;
    }

    public int getNroPool() {
        return nroPool;
    }

    public void setNroPool(int nroPool) {
        this.nroPool = nroPool;
    }

    public String getTextos() {
        return textos;
    }

    public void setTextos(String textos) {
        this.textos = textos;
    }

    public String getDebaja() {
        return debaja;
    }

    public void setDebaja(String debaja) {
        this.debaja = debaja;
    }

    public Date getFecalt() {
        return fecalt;
    }

    public void setFecalt(Date fecalt) {
        this.fecalt = fecalt;
    }

    public Date getFecmod() {
        return fecmod;
    }

    public void setFecmod(Date fecmod) {
        this.fecmod = fecmod;
    }

    public String getOalias() {
        return oalias;
    }

    public void setOalias(String oalias) {
        this.oalias = oalias;
    }

    public String getUltopr() {
        return ultopr;
    }

    public void setUltopr(String ultopr) {
        this.ultopr = ultopr;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public MovimientoTienda getPedido() {
        return pedido;
    }

    public void setPedido(MovimientoTienda pedido) {
        this.pedido = pedido;
    }

    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public List<ItemReserva> getItemReservas() {
        return itemReservas;
    }

    public void setItemReservas(List<ItemReserva> itemReservas) {
        this.itemReservas = itemReservas;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    //Importe total de la reserva que hará el cliente
    public BigDecimal getTotalReservaConIva(){

        if(itemReservas==null) return BigDecimal.ZERO;

        totalReservaConIVA = BigDecimal.ZERO;

        for(ItemReserva rs: itemReservas){

            totalReservaConIVA = totalReservaConIVA.add(rs.getTotalLineaConIVA()).setScale(2,2);
        }

        return totalReservaConIVA;
    }
    
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ReservasPool other = (ReservasPool) obj;
        if (this.nroPool != other.nroPool) {
            return false;
        }
        if ((this.nroCuenta == null) ? (other.nroCuenta != null) : !this.nroCuenta.equals(other.nroCuenta)) {
            return false;
        }
        return true;
    }

    public BigDecimal getCantidad() {

        BigDecimal cantidad = BigDecimal.ZERO;

        if(itemReservas==null) return cantidad;

        for(ItemReserva ir: itemReservas){
            cantidad = cantidad.add(ir.getCantidad());
        }
        return cantidad;

    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + this.nroPool;
        hash = 97 * hash + (this.nroCuenta != null ? this.nroCuenta.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "ReservasPool{" + "nroPool=" + nroPool + "nroCuenta=" + nroCuenta + '}';
    }
}
